package com.yz.newspaper.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yz.newspaper.pojo.po.ManuscriptIssue;
import com.yz.newspaper.pojo.result.Result;
import com.yz.newspaper.pojo.unitPo.PageBean;
import com.yz.newspaper.service.IManuscriptIssueService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *稿件详情记录
 * @author maike
 * @since 2024-04-23
 */
@RestController
@RequestMapping("/manuscript")
@Slf4j
public class ManuscriptIssueController {

    @Autowired
    private IManuscriptIssueService manuscriptIssueService;

    /**
     * 添加稿件记录
     * 记得看看哪些是必填的
     * @return
     */
    @PostMapping
    public Result<String> addManuscript(@RequestBody ManuscriptIssue manuscriptIssue){
        if(manuscriptIssueService.addManuscript(manuscriptIssue)>0){
            return Result.success("提交成功",null);
        }
        return Result.error("提交失败");
    }

    /**
     * 获取所有稿件记录
     * cxyx
     *
     * @return
     */
    @GetMapping("/list")
    public Result getList(String username, String phone, Integer year, Integer month, Integer pageSize, Integer pageNum) {
        log.info("username:{},phone:{},pageSize:{},pageNum:{}", username, phone, year, month, pageSize, pageNum);
        PageBean<ManuscriptIssue> pageBean = manuscriptIssueService.getAll(username, phone, year, month, pageSize, pageNum);
        return Result.success(pageBean);
    }

    @GetMapping("/{userId}")
    @ApiOperation("获取个人稿件")
    public Result getManuscript(@PathVariable Integer userId) {
        LambdaQueryWrapper<ManuscriptIssue> queryWrapper = new LambdaQueryWrapper<ManuscriptIssue>();
        queryWrapper.eq(ManuscriptIssue::getUserId, userId);
        ManuscriptIssue manuscriptIssue = manuscriptIssueService.getOne(queryWrapper);
        return Result.success(manuscriptIssue);
    }

    @GetMapping("/{id}")
    @ApiOperation("获取稿件详情")
    public Result getManuscriptDetail(@PathVariable Integer id) {
        ManuscriptIssue manuscriptIssue = manuscriptIssueService.getById(id);
        return Result.success(manuscriptIssue);
    }

    @DeleteMapping("/{id}")
    @ApiOperation("删除稿件")
    public Result deleteManuscript(@PathVariable Integer id) {
        if (manuscriptIssueService.removeById(id)) {
            return Result.success("删除成功", null);
        }
        return Result.error("删除失败");
    }

    @PutMapping
    @ApiOperation("修改稿件")
    public Result updateManuscript(@RequestBody ManuscriptIssue manuscriptIssue) {
        if (manuscriptIssueService.updateById(manuscriptIssue)) {
            return Result.success("修改成功", null);
        }
        return Result.error("修改失败");
    }
}
